<template>
	<view class="content">
		<!-- 测试 -->
		<view class="content-money">
			<text>￥</text> <text>{{ payData.total_amount ? (payData.total_amount / 100).toFixed(2) : '0.00'   }}</text>
		</view>
		<view class="content-info">
			<view class="content-info-single">
			   <text>单号</text>
			   <text>{{ payData.pay_trace_no || ''  }}</text>
			</view>
			<view class="content-info-singles">
			   <text>商品</text>
			   <text>{{ payData.order_body || '' }}</text>
			</view>
		</view>
		<view class="content-pay" @click="Bpay">
			<text>{{ payData.order_body ? '支付' :'确定' }}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				payData:{},
				wxPayData:{},
				http:'https://dpay.zazpay.cn', //正式
				// http:'https://test.zazpay.cn', //体验
			}
		},
		onLoad(v) {
			console.log(this.http)
			if(v.mch_no){
			  this.payData = {
			  	appid:'wx421cd2ca5e1e27f5',
			  	// secret:v.secret,
				inst_no:v.inst_no || '',
			  	terminal_no:v.terminal_no || '', //终端号或设备SN
			  	total_amount:v.total_amount || '', // 金额
				mch_no:v.mch_no || '', // 商户号
				shop_no: v.shop_no  || '' ,// 门店号
			  	order_body: decodeURIComponent(decodeURIComponent(v.order_body)) || '',  //商品名称
			  	attach:v.attach || '', // 自定义参数
			  	pay_trace_no:v.pay_trace_no || '', //单号
				notify_url:decodeURIComponent(v.notify_url) || '', //回调地址
				front_url:decodeURIComponent(v.front_url) || '', //支付完成返回前端地址
				pay_type:'400'
			  }	
			  this.getOpenId()
			}
			
		},
		methods: {
			getOpenId(){
				let that = this
				uni.showLoading({
					title:'加载中'
				})
				 uni.login({
				 	success(e) {
						 uni.request({
							url:`${that.http}/xypay/jsapi/getMiniOpenid`,
							method: "GET",
							data:{code:e.code,appid:'wx421cd2ca5e1e27f5',mch_no:that.payData.mch_no},
							success(res) {
								console.log(res)
								if(res.data.code == 200){
									that.payData.open_id = res.data.message
									that.getPayData()
									uni.hideLoading()
								}
							},
							fail(err) {
								console.log(err)
							}
					 })
				 	}
				 })
			},
			getPayData(){
				// const data = {pay_type:'400' , terminal_no:'' , total_amount:'' , open_id:this.openId,order_body:'',attach:'' , pay_trace_no:''}
				let that = this
				uni.request({
					url:`${that.http}/xypay/jsapi/minipay`,
									method:'POST',
									data:this.payData,
									success(res) {
										console.log('wxPayData',res)
										if(!res.data.paySign){
											uni.showToast({
												title:res.data.return_msg,
												icon:'none',
												duration:5000
											})
										}else{
											that.wxPayData = res.data
											that.Bpay('top')
										}
										
					}
				})
			},
             Bpay(type){
				 if(!this.wxPayData.signType) {
					 if(!this.payData.appid){
						 uni.redirectTo({
						 	url:'/pages/index/result' 
						 })
					 }
				 }else{
					 console.log(type)
					 if(type != 'top') return
					let that = this
					uni.requestPayment({
						 provider: 'wxpay',
						 timeStamp: this.wxPayData.timeStamp,	
						 nonceStr: this.wxPayData.nonceStr,
						 package: this.wxPayData.packages,
						 signType: this.wxPayData.signType,
						 paySign: this.wxPayData.paySign,
						 success(res) {
							console.log(res)
						 	uni.redirectTo({
						 		url:'/pages/index/result?trade_no=' +that.wxPayData.trade_no + '&mch_no='+ that.wxPayData.mch_no
						 	})
						 },
						 fail(err) {
						 	console.log(err)
						 }
					}) 
				 }
				  // https://dpay.zazpay.cn/xypay/jsapi/minipay
				 // return
				 // uni.request({
					//  url: 'http://192.168.3.35:8183/sqx_fast/app/wxPay/payMoney',
					//  method: "POST",
					//  success(res) {
					//  	console.log(res)
					//  }
				 // })
				 // uni.requestPayment({
					//  provider: 'wxpay',
					//  timeStamp: '1704863383',	
					//  nonceStr: 'f82dd2b270e44a9093e6f5510e25a5ef',
					//  package: 'prepay_id=wx1013094389978172b2fc8f06d148e80000',
					//  signType: 'RSA',
					//  paySign: 'JNYCXf7KznM9uFKj2GH2p0/7gFuk80BIKWgKr1EyDUd7/fj2JJhtjLNTjiONqiz1dygx5QEsBH/4PZVd6EAr8yphZRRmnLhbemhpaVm9RqmD7zt/vQJJkdERDFLmutrxD8kDZ53/fB+nb/ETiI9biUl+2jYK5z1YagksU6Dv61zqiMRSkzvCtGejjBJhlMNkBHg/oWJ+FJFNDNPNiRc4r+uXUYOfjraj+QvS5hcuBSf3dMoHkPzsf8+nZvKHZ6+91q5KejeW4dJLUT/82QiAvq+/Z6nLbacrxWbcKNhUsKR5QcdJtgDa7NsO9Js4brP13yJebq2+QjxUsvhoagdkDg==',
					//  success(res) {
					//  	console.log(res)
					//  },
					//  fail(err) {
					//  	console.log(err)
					//  }
				 // })
				
			 }
		}
	}
</script>

<style scoped lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		&-money{
			margin: 120rpx 0;
			text:nth-child(1){
				font-size: 28rpx;
				font-weight: 600;
			}
			text:nth-child(2){
				font-size: 38rpx;
				font-weight: 600;
			}
		}
		&-info{
			&-single{
				width: calc(100vw - 64rpx);
				padding: 32rpx;
				border-top: 1rpx solid rgb(227, 227, 2777);
			    border-bottom: 1rpx solid rgb(227, 227, 2777);
			    display: flex;
				align-items: center;
				justify-content: space-between;
				text{
					font-size: 28rpx;
					font-weight: 700;
				}
			}
			&-singles{
				width: calc(100vw - 64rpx);
				padding: 32rpx;
				border-bottom: 1rpx solid rgb(227, 227, 2777);
				display: flex;
				align-items: center;
				justify-content: space-between;
				text{
					font-size: 28rpx;
					font-weight: 700;
				}
			}
		}
		&-pay{
		   width: calc(100vw - 64rpx);
		   padding: 28rpx 0;
		   background-color:#42b983 ;
		   border-radius: 12rpx;
		   margin-top: 100rpx;
		   text-align: center;
		   font-weight: 700;
		   color: #fff;
		}
	}

	
</style>
